package com.rimi.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rimi.bbs.bean.Card;
import com.rimi.bbs.bean.CardStatus;
import com.rimi.bbs.mapper.CardMapper;
import com.rimi.bbs.mapper.CardTypeMapper;
import com.rimi.bbs.mapper.UserMapper;
import com.rimi.bbs.service.CardService;
import com.rimi.bbs.util.IntegerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jerry
 * @since 2019-02-24
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements CardService {

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CardTypeMapper cardTypeMapper;

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Override
    public IPage<Card> getCardList(Card card, Integer nowPage, Integer pageSize) {
        QueryWrapper<Card> queryWrapper=new QueryWrapper<>();
        //倒序查询
        queryWrapper.orderByDesc("card_id","card_create_time");
        if(card!=null){
            //帖子类型
            if(IntegerUtils.isNotEmptyAndNotZero(card.getCardTypeId())){
                queryWrapper.eq("card_type_id",card.getCardTypeId());
            }
            //技术类型
            if(IntegerUtils.isNotEmptyAndNotZero(card.getTechnicalId())){
                queryWrapper.eq("technical_id",card.getTechnicalId());
            }
            //帖子状态
            if(!StringUtils.isEmpty(card.getCardStatus())){
                //不是综合的情况下
                if(!CardStatus.ALL.equals(card.getCardStatus())){
                    queryWrapper.eq(card.getCardStatus().getColumn(),card.getCardStatus().getCode());
                }
            }
        }
        IPage<Card> cards = cardMapper.selectPage(new Page<Card>(nowPage, pageSize), queryWrapper);
        for (Card c : cards.getRecords()) {
            c.setUser(userMapper.selectById(c.getUserId()));
            c.setCardType(cardTypeMapper.selectById(c.getCardTypeId()));
        }
        return cards;
    }


    @Override
    public Card getCardById(Integer id){
        Card card = cardMapper.selectById(id);
        card.setUser(userMapper.selectById(card.getUserId()));
        card.setCardType(cardTypeMapper.selectById(card.getCardTypeId()));
        return card;
    }

}
